package DatabaseManager;

import java.sql.SQLException;

import Helper.Misc;
import Other.*;

public class CharacterTable extends Table {

	/**
	 * The function builds a table object (doesn't actually create it in the
	 * database)
	 * 
	 * @param db_mgr
	 *            - the database manager
	 * @throws SQLException
	 *             - thrown in case of an sql exception
	 * @throws ClassNotFoundException
	 *             - thrown in case of a class not found exception
	 */
	public CharacterTable(DBManager db_mgr) throws SQLException,
			ClassNotFoundException {
		super(db_mgr);
		// DB scheme is constant
		table_name = "fictional_character";
		primaryKey = "character_id";
		String[] tmp = { "name", "appears_in_these_fictional_universes",
				"hit_points", "strength", "character_id" };
		columns = tmp;

		foreignKey = new String[columns.length];
		for (int i = 0; i < foreignKey.length; i++)
			foreignKey[i] = null;

		dataTypes = new String[columns.length];
		dataTypes[0] = "VARCHAR(100)";
		dataTypes[1] = "TEXT";
		for (int i = 2; i < 5; i++)
			dataTypes[i] = "INT";

		boolean[] tmp1 = { true, true, false, false, false };
		isDataTypeNull = tmp1;
		isMyISAM = true; // already during creation
	}


	/**
	 * Inserts a card to the db and returns its id
	 * @param card - card to insert
	 * @return inserted card's id
	 * @throws SQLException
	 */
	public int insertCardAndGetID(ServerCard card) throws SQLException {
		return db_mgr.InsertTableAndReturnID(table_name, columns, new String[] {Misc.correctString(card.getName()), "", String.valueOf(card.getDefense()),
				String.valueOf(card.getAttack())}, primaryKey);
	}
	
	@Override
	public void UpdateAll(String[] values, String where) throws SQLException {
		String[] cols = {"name", "hit_points", "strength"};
		db_mgr.UpdateTable(table_name, cols, values, where);
	}
	
}
